# -*- coding: utf-8 -*-
"""
Created on Sun Mar 31 21:53:53 2019

@author: AINIVERSHERRY
"""

import numpy as np
import pandas as pd
data = pd.read_csv(r'G:\Software_Files\Python_Files\loans_2018q1_pre.csv')

#特征抽象，把贷款状态标签LoanStatus编码为：违约=1, 正常=0
pd.value_counts(data['loan_status'])

def coding(col, codeDict):
    colCoded = pd.Series(col, copy = True)
    for key, value in codeDict.items():
        colCoded.replace(key, value, inplace = True)
    return colCoded

data['loan_status'] = coding(data['loan_status'], {'Current':0,\
     'Fully Paid':0, 'In Grace Period': 1, 'Late (31-120 days)': 1,\
     'Late (16-30 days)': 1, 'Default': 1, 'Charged Off':1})

data['loan_status']
pd.value_counts(data['loan_status'])
data1 = data.iloc[0:107863, :]
data1['loan_status'] = data1['loan_status'].apply(lambda x: float(x))

#贷款状态分布可视化
import matplotlib.pyplot as plt
labels = ['Normal', 'Default']
fracs = [len(data1['loan_status']) - data1['loan_status'].sum(), data1['loan_status'].sum()]
explode = [0, 0.1]
colors = 'lightgreen', 'gold'
plt.axes(aspect = 1)  #设置x和y轴比例为1时，为正圆
plt.pie(x = fracs, labels = labels, explode = explode, colors = colors, autopct='%3.1f %%')
plt.show()

#筛选数据类型为object的变量
objuect_columns = data1.select_dtypes(include = ['object'])

#分箱
mapping_dict = {
        'emp_length': {
                '10+ years': 10,
                '9 years': 9,
                '8 years': 8,
                '7 years': 7,
                '6 years': 6,
                '5 years': 5,
                '4 years': 4,
                '3 years': 3,
                '2 years': 2,
                '1 year': 1,
                '< 1 year': 0,
                'Unknown': 0
                       },
        'grade': {
                'A': 1,
                'B': 2,
                'C': 3,
                'D': 4,
                'E': 5,
                'F': 6,
                'G': 7
                 }
                }
data2 = data1.replace(mapping_dict)
data2[['emp_length', 'grade']].head()

#one-hot encoding
one_hot_columns = ['term', 'home_ownership', 'verification_status', \
                   'purpose', 'application_type', 'disbursement_method',\
                   'debt_settlement_flag']
dummy_df = pd.get_dummies(data2[one_hot_columns])
data3 = pd.concat([data2, dummy_df], axis = 1)  #axis = 1时，行对齐，将不同列名称合并
data3 = data3.drop(one_hot_columns, axis = 1)
data3.info()

#数值型变量数据标准化
numeric_columns = data3.drop('loan_status', axis = 1).select_dtypes(include = ['int64', 'float64']).columns

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()  #初始化缩放器
data3[numeric_columns] = sc.fit_transform(data3[numeric_columns])
data3[numeric_columns].head()

#特征选择
#构建特征变量X和目标变量Y
x_feature = list(data3.columns)
x_feature.remove('loan_status')
x_val = data3[x_feature]
y_val = data3['loan_status']

#Wrapper
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()  #建立logistic回归分类器
rfe = RFE(model, 20)  #通过递归选择特征，选择20个特征
rfe = rfe.fit(x_val, y_val)
col_wrapper = x_val.columns[rfe.support_]  #通过布尔值筛选首次降维后的变量

#Filter
import seaborn as sns
import matplotlib.pyplot as plt
colormap = plt.cm.viridis
plt.figure(figsize=(12,12))
plt.title('Pearson Correlation of Features', y=1.05, size=15)
sns.heatmap(data3[col_wrapper].corr(),linewidths=0.1,vmax=1.0, square=True,\
            cmap=colormap, linecolor='white', annot=True)

drop_col = ['funded_amnt', 'funded_amnt_inv', 'installment', 'out_prncp', \
            'out_prncp_inv', 'total_pymnt_inv', 'total_rec_prncp',\
            'total_rec_int', 'term_ 36 months', 'application_type_Individual',\
            'debt_settlement_flag_N']
col_filter = col_wrapper.drop(drop_col)  #剔除冗余特征

#再次查看剩余变量相关性
colormap = plt.cm.viridis
plt.figure(figsize=(12,12))
plt.title('Pearson Correlation of Features', y=1.05, size=15)
sns.heatmap(data3[col_filter].corr(),linewidths=0.1,vmax=1.0, square=True,\
            cmap=colormap, linecolor='white', annot=True)

data4 = data3[col_filter]
data4['loan_status'] = data['loan_status']
data4.to_csv(r'G:\Software_Files\Python_Files\loans_2018q1_feature.csv', index = False)



